package com.mmy.controller.restaurant;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.mmy.domain.constzone.TConstZone;
import com.mmy.domain.restaurant.TRestaurantOrder;
import com.mmy.domain.restaurant.VRestaurantBase;
import com.mmy.domain.restaurant.VRestaurantPlan;
import com.mmy.domain.scenic.VScenicBase;
import com.mmy.domain.sys.TSysUser;
import com.mmy.service.impl.constzone.ConstZoneServiceImpl;
import com.mmy.service.impl.restaurant.TRestaurantOrderServiceImpl;
import com.mmy.service.impl.restaurant.VRestaurantBaseServiceImpl;
import com.mmy.service.impl.restaurant.VRestaurantPlanServiceImpl;
import com.mmy.service.scenic.impl.TScenicBaseServiceImpl;

/**
 * 类的介绍：餐馆信息
 *
 * @author hanxl
 * @date 创建日期：2020-09-14
 * @version 1.0
 */

@Controller
public class VRestaurantBaseController {

	@Autowired
	VRestaurantBaseServiceImpl vRestaurantBaseServiceImpl;
	@Autowired
	TScenicBaseServiceImpl tScenicBaseServiceImpl;
	@Autowired
	ConstZoneServiceImpl constZoneServiceImpl;
	@Autowired
	TRestaurantOrderServiceImpl tRestaurantOrderServiceImpl;
	@Autowired
	VRestaurantPlanServiceImpl vRestaurantPlanServiceImpl;

	/**
	 * 函数功能说明： 跳转餐馆首页
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 韩现丽
	 * @date 创建日期2020-09-14
	 */
	@RequestMapping(value = "/restaurantindex")
	public String toRestaurantIndex(Model model) {
		try {

			List<VRestaurantBase> lstRestaurantBase = vRestaurantBaseServiceImpl.findAll();
			model.addAttribute("restaurantbaselist", lstRestaurantBase);

			List<VScenicBase> lstScenicBase = tScenicBaseServiceImpl.findAll();
			model.addAttribute("scenicBaselist", lstScenicBase);
			/* 查询所有区域 国家或者省市区 */
			List<TConstZone> db_TConstZoneProvince = constZoneServiceImpl.findAllProvince();
			model.addAttribute("newTConstZoneProvince", db_TConstZoneProvince);
			List<TConstZone> db_TConstZone = constZoneServiceImpl.findAllCity();
			model.addAttribute("newTConstZone", db_TConstZone);
			List<TConstZone> db_TConstZoneDistrict = constZoneServiceImpl.findAllDistrict();
			model.addAttribute("newTConstZoneDistrict", db_TConstZoneDistrict);

			return "webbackground/restaurantmanagement/restaurantindex";
		} catch (Exception e) {

		}
		return null;

	}

	/**
	 * 函数功能说明： 查询餐馆信息
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 韩现丽
	 * @date 创建日期2020-09-14
	 */
	@RequestMapping(value = "/searchrestaurant")
	public String FindRestaurantBase(HttpServletRequest request, Model model) {

		String strRestaurantName = request.getParameter("RestaurantName");
		String strZoneID = request.getParameter("ZoneID");
		String strRate = request.getParameter("Rate");
		String strScenicID = request.getParameter("ScenicID");
		List<VRestaurantBase> lstRestaurantBase = new ArrayList<VRestaurantBase>();
		if (strRestaurantName == "" && strZoneID == "" && strRate == "" && strScenicID == "") {
			lstRestaurantBase = vRestaurantBaseServiceImpl.findAll();

		} else {
			lstRestaurantBase = vRestaurantBaseServiceImpl.findBySearch(strRestaurantName, strZoneID, strRate,
					strScenicID);

		}
		model.addAttribute("restaurantbaselist", lstRestaurantBase);

		List<VScenicBase> lstScenicBase = tScenicBaseServiceImpl.findAll();
		model.addAttribute("scenicBaselist", lstScenicBase);
		/* 查询所有区域 国家或者省市区 */
		List<TConstZone> db_TConstZoneProvince = constZoneServiceImpl.findAllProvince();
		model.addAttribute("newTConstZoneProvince", db_TConstZoneProvince);
		List<TConstZone> db_TConstZone = constZoneServiceImpl.findAllCity();
		model.addAttribute("newTConstZone", db_TConstZone);
		List<TConstZone> db_TConstZoneDistrict = constZoneServiceImpl.findAllDistrict();
		model.addAttribute("newTConstZoneDistrict", db_TConstZoneDistrict);
		request.setAttribute("strRestaurantName", strRestaurantName);
		request.setAttribute("strRate", strRate);
		return "webbackground/restaurantmanagement/restaurantindex";

	}

	@RequestMapping(value = "/restaurantbaseinfo")
	public String ToRestaurantBaseInfo(HttpServletRequest request, Model model) {

		int iRestaurantID = Integer.parseInt(request.getParameter("iRestaurantID"));
		model.addAttribute("iRestaurantID", iRestaurantID);

		VRestaurantBase vRestaurantBase = vRestaurantBaseServiceImpl.findByRestaurantID(iRestaurantID);
		model.addAttribute("vRestaurantBase", vRestaurantBase);

		List<TRestaurantOrder> lstRestaurantOrder = tRestaurantOrderServiceImpl.findByRestaurantID(iRestaurantID);
		model.addAttribute("orderlist", lstRestaurantOrder);

		return "webbackground/restaurantmanagement/restaurantinfo";
	}

	@RequestMapping(value = "/orderrestaurantinfo")
	public String ToOrderRestaurantInfo(HttpServletRequest request, Model model) {

		int iID = Integer.parseInt(request.getParameter("iID"));

		TRestaurantOrder tRestaurantOrder = tRestaurantOrderServiceImpl.findByID(iID);
		model.addAttribute("tRestaurantOrder", tRestaurantOrder);

		String strOrderID = tRestaurantOrder.getOrderID();
		int iPlanID = Integer.parseInt(strOrderID.substring(9, 18));

		VRestaurantPlan vRestaurantPlan = vRestaurantPlanServiceImpl.findByPlanID(iPlanID);

		model.addAttribute("vRestaurantPlan", vRestaurantPlan);

		return "webbackground/restaurantmanagement/orderrestaurantinfo";
	}

//收款打印页面
	@RequestMapping(value = "/printrestaurant")
	public String ToPrintRestaurant(HttpServletRequest request, Model model) {

		int iID = Integer.parseInt(request.getParameter("iID"));

		TRestaurantOrder tRestaurantOrder = tRestaurantOrderServiceImpl.findByID(iID);
		model.addAttribute("tRestaurantOrder", tRestaurantOrder);

		return "webbackground/restaurantmanagement/printrestaurant";
	}

	// 退款打印页面
	@RequestMapping(value = "/printcancelrestaurant")
	public String ToPrintCancelRestaurant(HttpServletRequest request, Model model) {

		int iID = Integer.parseInt(request.getParameter("iID"));

		TRestaurantOrder tRestaurantOrder = tRestaurantOrderServiceImpl.findByID(iID);
		model.addAttribute("tRestaurantOrder", tRestaurantOrder);

		return "webbackground/restaurantmanagement/printcancelrestaurant";
	}

	/**
	 * 函数功能说明： 出纳审核
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期
	 */

	@RequestMapping(value = "/cashierrestaurant")
	public String ToCashierRestaurant(Model model) {
		try {

			List<VRestaurantBase> lstRestaurantBase = vRestaurantBaseServiceImpl.findAll();
			model.addAttribute("vRestaurantBaseList", lstRestaurantBase);

			return "OA/cashier/cashierrestaurant";
		} catch (Exception e) {

		}
		return null;

	}

	@RequestMapping(value = "/cashierrestaurantorder")
	public String ToCashierRestaurantOrder(HttpServletRequest request, Model model) {

		int iRestaurantID = Integer.parseInt(request.getParameter("iRestaurantID"));

		List<TRestaurantOrder> lstRestaurantOrder = tRestaurantOrderServiceImpl.findByRestaurantID(iRestaurantID);
		model.addAttribute("orderlist", lstRestaurantOrder);

		model.addAttribute("iRestaurantID", iRestaurantID);

		return "OA/cashier/cashierrestaurantorder";
	}

	@RequestMapping(value = "/entercheckcashierrestaurant")
	public String EnterCheckCashierRestaurant(HttpServletRequest request, HttpSession session, String checkList) {

		TSysUser tSysUser = (TSysUser) session.getAttribute("tSysUser");

		String[] checklist = checkList.split(",");
		List<Integer> lstCashierID = new ArrayList<>();
		List<TRestaurantOrder> lstCashier = new ArrayList<>();

		for (String str : checklist) {
			lstCashierID.add(Integer.parseInt(str));
			int i = Integer.parseInt(str);
			TRestaurantOrder tRestaurantOrder = tRestaurantOrderServiceImpl.findByID(i);

			if (tRestaurantOrder.getCashierState() == 0) {
				tRestaurantOrder.setCashierState((byte) 1);
			} else if (tRestaurantOrder.getCashierState() == 1) {
				tRestaurantOrder.setCashierState((byte) 2);
			} else {
				tRestaurantOrder.setCashierState((byte) 1);
			}
			tRestaurantOrder.setCashierDate(new Date());
			tRestaurantOrder.setCashierID(tSysUser.getUserID());

			lstCashier.add(tRestaurantOrder);
		}

		tRestaurantOrderServiceImpl.saveAll(lstCashier);

		return "OA/cashier/cashierrestaurantorder";

	}

//查询
	@RequestMapping(value = "/searchcashierrestaurant")
	public String FindCashierRestaurantOrder(HttpServletRequest request, Model model) {

		int iRestaurantID = Integer.parseInt(request.getParameter("iRestaurantID"));
		String strCashierState = request.getParameter("CashierState");
		if (strCashierState == "") {

			List<TRestaurantOrder> lstRestaurantOrder = tRestaurantOrderServiceImpl.findByRestaurantID(iRestaurantID);
			model.addAttribute("orderlist", lstRestaurantOrder);
			model.addAttribute("iRestaurantID", iRestaurantID);
			return "OA/cashier/cashierrestaurantorder";
		} else {
			List<TRestaurantOrder> lstRestaurantOrder = tRestaurantOrderServiceImpl
					.findByCashierAndRestaurantID(strCashierState, iRestaurantID);
			model.addAttribute("orderlist", lstRestaurantOrder);
			model.addAttribute("iRestaurantID", iRestaurantID);
			return "OA/cashier/cashierrestaurantorder";
		}

	}

	/**
	 * 函数功能说明：会计对账
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期
	 */

	@RequestMapping(value = "/accountrestaurant")
	public String ToAccountRestaurant(HttpServletRequest request, Model model) {

		List<VRestaurantBase> lstRestaurantBase = vRestaurantBaseServiceImpl.findAll();
		model.addAttribute("vRestaurantBaseList", lstRestaurantBase);

		return "OA/account/accountrestaurant";
	}

	@RequestMapping(value = "/accountrestaurantorder")
	public String ToAccountRestaurantOrder(HttpServletRequest request, Model model) {

		int iRestaurantID = Integer.parseInt(request.getParameter("iRestaurantID"));

		List<TRestaurantOrder> lstRestaurantOrder = tRestaurantOrderServiceImpl.findByRestaurantID(iRestaurantID);
		model.addAttribute("orderlist", lstRestaurantOrder);

		model.addAttribute("iRestaurantID", iRestaurantID);

		return "OA/account/accountrestaurantorder";
	}

	@RequestMapping(value = "/entercheckaccountrestaurant")
	public String EnterCheckAccountRestaurant(HttpServletRequest request, String checkList, HttpSession session) {

		TSysUser tSysUser = (TSysUser) session.getAttribute("tSysUser");

		String[] checklist = checkList.split(",");
		List<Integer> lstAccountID = new ArrayList<>();
		List<TRestaurantOrder> lstAccount = new ArrayList<>();

		for (String str : checklist) {
			lstAccountID.add(Integer.parseInt(str));
			int i = Integer.parseInt(str);
			TRestaurantOrder tRestaurantOrder = tRestaurantOrderServiceImpl.findByID(i);

			if (tRestaurantOrder.getAccountState() == 0) {
				tRestaurantOrder.setAccountState((byte) 1);
			} else if (tRestaurantOrder.getAccountState() == 1) {
				tRestaurantOrder.setAccountState((byte) 2);
			} else {
				tRestaurantOrder.setAccountState((byte) 1);
			}
			tRestaurantOrder.setAccountDate(new Date());
			tRestaurantOrder.setAccountID(tSysUser.getUserID());

			lstAccount.add(tRestaurantOrder);
		}

		tRestaurantOrderServiceImpl.saveAll(lstAccount);

		return "OA/account/accountrestaurantorder";

	}

//	结账汇款
	@RequestMapping(value = "/entercheckremittancerestaurant")
	public String EnterCheckRemittanceRestaurant(HttpServletRequest request, String checkList) {

		String[] checklist = checkList.split(",");
		List<Integer> lstRemittanceID = new ArrayList<>();
		List<TRestaurantOrder> lstRemittance = new ArrayList<>();

		for (String str : checklist) {
			lstRemittanceID.add(Integer.parseInt(str));
			int i = Integer.parseInt(str);
			TRestaurantOrder tRestaurantOrder = tRestaurantOrderServiceImpl.findByID(i);

			if (tRestaurantOrder.getRemittanceState() == 0) {
				tRestaurantOrder.setRemittanceState((byte) 1);
			} else {
				tRestaurantOrder.setRemittanceState((byte) 0);
			}

			lstRemittance.add(tRestaurantOrder);
		}

		tRestaurantOrderServiceImpl.saveAll(lstRemittance);

		return "OA/account/accountrestaurantorder";

	}

//查询
	@RequestMapping(value = "/searchaccountrestaurant")
	public String FindAccountRestaurant(HttpServletRequest request, Model model) {

		int iRestaurantID = Integer.parseInt(request.getParameter("iRestaurantID"));
		String strAccountState = request.getParameter("AccountState");

		if (strAccountState == "") {
			List<TRestaurantOrder> lstRestaurantOrder = tRestaurantOrderServiceImpl.findByRestaurantID(iRestaurantID);
			model.addAttribute("orderlist", lstRestaurantOrder);
			model.addAttribute("iRestaurantID", iRestaurantID);

			return "OA/account/accountrestaurantorder";
		} else {
			List<TRestaurantOrder> lstRestaurantOrder = tRestaurantOrderServiceImpl
					.findByAccountAndRestaurantID(strAccountState, iRestaurantID);
			model.addAttribute("orderlist", lstRestaurantOrder);
			model.addAttribute("iRestaurantID", iRestaurantID);

			return "OA/account/accountrestaurantorder";
		}

	}

}
